//
//  XS_KCPrice.swift
//  XueDao
//
//  Created by 韩云智 on 2020/12/29.
//

import UIKit
import RxCocoa
import RxSwift

class XS_KCPrice: UIView {
    lazy var disposeBag = DisposeBag()
    lazy var modelPS = PublishSubject<(String?, String?, String?)>()
    override init(frame: CGRect) {
        super.init(frame: frame)
        upView()
    }
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func upView() {
        backgroundColor = #colorLiteral(red: 1, green: 1, blue: 1, alpha: 1)
        
        let label = UILabel.xsLabel(size: .scale*14, fontName: .regular, color: #colorLiteral(red: 0.6666666667, green: 0.6941176471, blue: 0.7176470588, alpha: 1))
        addSubview(label)
        label.snp.makeConstraints({
            $0.left.equalTo(.scale*16)
            $0.top.bottom.right.equalToSuperview()
        })
        
        let oLab = UILabel()
        addSubview(oLab)
        oLab.snp.makeConstraints({
            $0.right.equalTo(-.scale*16)
            $0.bottom.equalTo(-.scale * 16)
        })
        
        let pLab = UILabel()
        addSubview(pLab)
        pLab.snp.makeConstraints({
            $0.right.equalTo(-.scale*16)
            $0.bottom.equalTo(oLab.snp.top)
        })
        
        modelPS.subscribe(onNext: { people, price, oldPrice in
            if let people = people {
                label.text = "\(people)"
            } else {
                label.text = nil
            }
            if let price = price {
                let text = NSMutableAttributedString()
                if price.count == 0 || price == "0" {
                    let attr = NSMutableAttributedString(string: "免费")
                    attr.yy_font = .xsFont(name: .medium, .scale*20)
                    text.append(attr)
                } else {
                    do {
                        let attr = NSMutableAttributedString(string: "￥")
                        attr.yy_font = .xsFont(name: .medium, .scale*16)
                        text.append(attr)
                    }
                    do {
                        let attr = NSMutableAttributedString(string: "\(price)")
                        attr.yy_font = .xsFont(name: .medium, .scale*24)
                        text.append(attr)
                    }
                }
                text.yy_color = #colorLiteral(red: 0.9294117647, green: 0.3960784314, blue: 0.2470588235, alpha: 1)
                text.yy_alignment = .right
                pLab.attributedText = text
            } else {
                pLab.attributedText = nil
            }
            if let oldPrice = oldPrice, oldPrice.count > 0 {
                let isVip = XS_Singleton.shared.infoModel?.userBO.userTypeEnum() == XS_UserType.vip
                let text = NSMutableAttributedString(string: isVip ? (Double(oldPrice)! > 0 ? "原价: ￥\(oldPrice)" : "") : (Double(oldPrice)! > 0 ? "会员价: ￥\(oldPrice)" : "会员价: 免费"))
//                text.yy_strikethroughStyle = .single
//                text.yy_strikethroughColor = #colorLiteral(red: 0.6666666667, green: 0.6941176471, blue: 0.7176470588, alpha: 1)
                text.yy_color = #colorLiteral(red: 0.6666666667, green: 0.6941176471, blue: 0.7176470588, alpha: 1)
                text.yy_alignment = .right
                oLab.attributedText = text
            } else {
                oLab.attributedText = nil
            }
        }).disposed(by: disposeBag)
    }

}
